package com.tvunetworks.center.user.service.impl;

import com.tvunetworks.center.common.constant.LocalConstant;
import com.tvunetworks.center.user.model.BaseBean;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @program: tvucc-user
 * @description: BaseService
 * @author lebronchen
 * @param <T> t
 * @create: 2019-02-28 16:38
 **/
@ConditionalOnBean(SqlSessionFactory.class)
public class BaseService<T extends BaseBean> {

    @Autowired
    private Mapper<T> mapper;

    /**
     *
     * @param id id
     * @return  t
     */
    public T selectByPrimaryKey(Object id) {
        return this.mapper.selectByPrimaryKey(id);
    }

    /**
     *
     * @param id id
     * @return t
     */
    public T selectExistByPrimaryKey(Object id) {
        T t = this.mapper.selectByPrimaryKey(id);
        if ( t== null ||t.getDeleteFlag() == LocalConstant.FLAG_TRUE) {
            return null;
        }
        return t;
    }

    /**
     * 查询所有对象
     *
     * @return List
     */
    public List<T> selectAll() {
        return this.mapper.select(null);
    }

    /**
     * 查询所有对象
     *
     * @return List
     */
    public List<T> selectExistAll() {
        List<T> resultList = new ArrayList<>();
        List<T> list = this.mapper.select(null);
        if (list == null || list.size() == 0) {
            return null;
        }
        for (T t : list) {
            if (t.getDeleteFlag() == LocalConstant.FLAG_FALSE) {
                resultList.add(t);
            }
        }
        return resultList;
    }

    /**
     * 根据条件查询唯一的一条记录
     * @param record record
     * @return t
     */
    public T selectOne(T record) {
        List<T> list = this.selectList(record);
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list.get(0);
    }

    /**
     * 根据条件查询未删除的唯一一条记录
     * @param record record
     * @return t
     */
    public T selectExistOne(T record) {
        record.setDeleteFlag(LocalConstant.FLAG_FALSE);
        return selectOne(record);
    }

    /**
     * 根据条件查询list数据
     *
     * @param record record
     * @return t
     */
    public List<T> selectList(T record) {
        return this.mapper.select(record);
    }

    /**
     * 根据条件查询未删除的list数据
     *
     * @param record  record
     * @return t
     */
    public List<T> selectExistList(T record) {
        record.setDeleteFlag(LocalConstant.FLAG_FALSE);
        return this.mapper.select(record);
    }

    /**
     * 添加记录
     *
     * @param record record
     * @return t
     */
    public Integer insert(T record) {
        // 把创建时间，和修改时间初始化进去。
        record.setId(getUUID());
        record.setCreateTime(System.currentTimeMillis());
        record.setUpdateTime(record.getCreateTime());
        record.setDeleteFlag(LocalConstant.FLAG_FALSE);
        return this.mapper.insert(record);

    }

    /**
     * 修改对象，如果属性是null，也进行修改
     *
     * @param record  record
     * @return t
     */
    public Integer update(T record) {
        record.setUpdateTime(System.currentTimeMillis());
        return this.mapper.updateByPrimaryKey(record);
    }

    /**
     * 修改对象，如果属性是null，则不进行修改
     *
     * @param record record
     * @return t
     */
    public Integer updateSelective(T record) {
        record.setCreateTime(null);// 强制把创建时间设置成null，不让修改
        record.setUpdateTime(System.currentTimeMillis());
        return this.mapper.updateByPrimaryKeySelective(record);
    }

    /**
     *
     * @param id id
     * @return Integer
     */
    public Integer deleteById(Object id) {
        return this.mapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据主键逻辑删除
     *
     * @param id id
     * @return t
     */
    public Integer deleteLogicById(Object id) {
        T record = this.selectByPrimaryKey(id);
        record.setDeleteFlag(LocalConstant.FLAG_TRUE);
        return this.updateSelective(record);
    }

    /**
     * 根据ids 删除多个对象
     *
     * @param clazz
     *            ，实体对应的class
     * @param property
     *            实体类中主键属性名
     * @param ids
     *            要删除的主键的值
     * @return t
     */
    public Integer deleteByIds(Class<T> clazz, String property, List<Object> ids) {
        Example example = new Example(clazz);
        // 第一个参数，实体类的属性
        example.createCriteria().andIn(property, ids);
        return this.mapper.deleteByExample(example);
    }

    /**
     * 根据条件删除数据
     *
     * @param record record
     * @return t
     */
    public Integer deleteByWhere(T record) {
        return this.mapper.delete(record);
    }

    /**
     * 根据条件，逻辑删除
     *
     * @param record record
     * @return t
     */
    public Integer deleteLogicByWhere(T record) {
        List<T> list = this.selectList(record);
        if (list != null && list.size() > 0) {
            for (T t : list) {
                t.setDeleteFlag(LocalConstant.FLAG_TRUE);
                this.updateSelective(t);
            }
            return list.size();
        }
        return null;

    }

    /**
     *
     * @return String
     */
    public String getUUID() {
        return UUID.randomUUID().toString().replace("-", "");
    }
}
